package com.ltt.service.impl;

import com.ltt.dao.pojo.Blog;
import com.ltt.dao.pojo.FileItems;
import com.ltt.dao.pojo.Likes;
import com.ltt.dao.vo.params.DeleteBlogParams;
import com.ltt.dao.vo.params.NumsParams;
import com.ltt.mapper.BlogMapper;
import com.ltt.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author ‘李天泰’
 * @version 1.0
 * @description: TODO
 * @date 2023/5/10 21:47
 */
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;

    @Override
    public int insertBlog(Blog blog) {
        int id = blogMapper.insertBlog(blog);
        return id;
    }

    @Override
    public Blog findBlog(int id) {
        return blogMapper.findBlog(id);
    }

    @Override
    public void updateBlog(Blog blog) {
        blogMapper.updateBlog(blog);
    }

    @Override
    public List<Blog> findBlogByOpenid(String openid) {
        List<Blog> blogList = blogMapper.findBlogByOpenid(openid);
        return blogList;
    }

    @Override
    public List<Blog> getAllBlog() {
        return blogMapper.getAllBlog();
    }

    @Override
    public List<Blog> findBlogByContent(String content) {
        return blogMapper.findBlogByContent(content);
    }

    @Override
    public Likes findLikeBlog(Likes like) {
        return blogMapper.findLikeBlog(like);
    }

    @Override
    public void addLikes(Likes likes) {
        System.out.println(likes);
        Likes likeBlog = blogMapper.findAllLikeBlog(likes);
        System.out.println(likeBlog);
        if(likeBlog == null){
            blogMapper.addLikes(likes);
        }else {
            blogMapper.updateLikes(likes);
        }
        Blog blog = blogMapper.findBlog(likes.getBlogId());
        NumsParams numsParams = new NumsParams();
        numsParams.setBlogId(likes.getBlogId());
        numsParams.setLikesNums(blog.getLikeNums()+1);
        blogMapper.updateLikesNums(numsParams);
    }

    @Override
    public void cancelLikes(Likes likes) {
        blogMapper.cancelLikes(likes);
        Blog blog = blogMapper.findBlog(likes.getBlogId());
        NumsParams numsParams = new NumsParams();
        numsParams.setBlogId(likes.getBlogId());
        numsParams.setLikesNums(blog.getLikeNums()-1);
        blogMapper.updateLikesNums(numsParams);
    }

    @Override
    public void deleteBlog(DeleteBlogParams deleteBlogParams) {
        blogMapper.deleteBlog(deleteBlogParams);
    }

    @Override
    public HashSet<Blog> getAllBlogsBy(String openid, String content, String label){
        List<Blog> res = new ArrayList<Blog>();
        if(openid.length() > 0){
            List<Blog> blogs = blogMapper.getAllBlogsByOpenId(openid);
            if(blogs.size() > 0) {
                res.addAll(blogs);
            }
        }
        if(content.length() > 0){
            List<Blog> blogss = blogMapper.getAllBlogsByOpenContent(content);
            if(blogss.size() > 0) {
                res.addAll(blogss);
            }
        }
        if(label.length() > 0) {
            List<Blog> blogsss = blogMapper.getAllBlogsByLabel(label);
            if(blogsss.size() > 0) {
                res.addAll(blogsss);
            }
        }

        if(res.size() == 0){
            List<Blog> blogsss = blogMapper.getAllBlogsByLabel("");
        }
        HashSet<Blog> set = new HashSet<>(res);
        return set;
    }
}
